/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
 //递归，后序遍历思想
// class Solution {
// public:
//     int maxDepth(TreeNode* root) {
//         if(root==nullptr) return 0;
//         return max(maxDepth(root->left),maxDepth(root->right))+1;
//     }
// };

//非递归，层序遍历
class Solution{
    public:
        int maxDepth(TreeNode* root)
        {
            if(!root) return 0;
            vector<TreeNode*> que;
            que.push_back(root);
            int ans=0;
            while(!que.empty())
            {
                vector<TreeNode*> tmp;
                for(auto node:que)
                {
                    if(node->left!=nullptr) tmp.push_back(node->left);
                    if(node->right!=nullptr) tmp.push_back(node->right);
                }
                que=tmp;
                ans++;
            }
            return ans;
        }
};
<Paste>
